<template>
	<view class="pos-r flex-r flex-wrap flex-start">
		<view class="cu-btn round mar2" 
		:class="isInArray('/api_/departments/' + user.id,value)?'bg-green':'line-green'"
		@tap="check('/api_/departments/' + user.id)" 
		v-for="user in users">
			<text class="padr1">{{user.name}}</text>
			<ripple></ripple>
		</view>
	</view>
</template>

<script>
	export default {
		props:{
			value:{
				type:Array,
				default:[]
			}
		},
		data() {
			return {
				loading:false,
				users:[]
			}
		},
		methods: {
			getAll(){
				this.loading = true
				this.$api.get("/api/departments",{limit:1000},res=>{
				this.loading = false
					// this.log(res)
					this.users = res.d
				})
			},
			check(user){
				// this.log(user)
				let checkedUsers = this.value
				if(this.$com.isInArray(user,checkedUsers)){
					//剔除
					checkedUsers = this.$com.arrayDelValue(checkedUsers,user)
				}else{
					//添加到已选
					checkedUsers.push(user)
				}
				this.$emit('input',checkedUsers)
			},
			isInArray(val,array){
				return this.$com.isInArray(val,array)
			}
		},
		mounted() {
			
			this.getAll()
		},
		watch:{
			
		}
	}
</script>

<style>

</style>
